<?php

namespace app\service;
use app\model\UserMoney;
class UserMoneyService
{
    private $userMoney;
    public function __construct(){
        $this->userMoney = new UserMoney;
    }

    public function list($params)
    {
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $where = [];
        if(!empty($params['username'])) $where[] = ['member.username', 'like', '%'.trim($params['username']).'%'];
        if(!empty($params['phone'])) $where[] = ['member.phone', 'like', '%'.trim($params['phone']).'%'];
        if(isset($params['type']) && $params['type']!='') $where[] = ['user_money.type', '=', $params['type']];

        $result = $this->userMoney->join('member', 'user_money.member_id = member.id', 'LEFT')
            ->join('admin_user', 'user_money.admin_id = admin_user.id', 'LEFT')
            ->field('user_money.*, member.nickname, member.username,member.phone,admin_user.username as admin_username')
            ->order(['user_money.id'=>'DESC'])
            ->where($where)->paginate([
                'list_rows'=> $pageSize,
                'page' => $page
            ])->append(['type_text']);

        return $result;
    }
    public function add($data)
    {
        $userMoney = $this->userMoney->create($data);
        return $userMoney;
    }

    public function apiList($params)
    {
        $member_id  = request()->_mid;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $where []= ['member_id','=',$member_id];
        if(!empty($params['username'])) $where[] = ['member.username', 'like', '%'.trim($params['username']).'%'];
        if(!empty($params['phone'])) $where[] = ['member.phone', 'like', '%'.trim($params['phone']).'%'];
        if(isset($params['type']) && $params['type']!='') $where[] = ['user_money.type', '=', $params['type']];

        $result = $this->userMoney->join('member', 'user_money.member_id = member.id', 'LEFT')
            ->field('user_money.*, member.nickname, member.username,member.phone')
            ->order(['user_money.id'=>'DESC'])
            ->where($where)->paginate([
                'list_rows'=> $pageSize,
                'page' => $page
            ])->append(['type_text']);

        return $result;
    }
}
